﻿using System;
using SharpSqlAnalyzer.nodes;
using SharpSqlAnalyzer.pp.para;
using SharpSqlAnalyzer.pp.para.styleenums;
using SharpSqlAnalyzer.pp.utils;

namespace SharpSqlAnalyzer.pp.processor.type.comm
{
	// Token: 0x02000252 RID: 594
	public class ColumnlistCommaProcessor : AbstractProcessor
	{
		// Token: 0x0600132A RID: 4906 RVA: 0x0031D198 File Offset: 0x0031B398
		public static object[] getNextNotEmptyNode(TParseTreeNodeList list, int startPos)
		{
			for (int i = startPos; i < list.size(); i++)
			{
				TParseTreeNode element = list.getElement(i);
				if (element.startToken != null)
				{
					return new object[]
					{
						element,
						i
					};
				}
			}
			return new object[]
			{
				null,
				-1
			};
		}

		// Token: 0x0600132B RID: 4907 RVA: 0x0031D1EC File Offset: 0x0031B3EC
		public static void processColumns(GFmtOpt opt, TLinefeedsCommaOption p, TAlignStyle alignStype, TParseTreeNodeList columns)
		{
			if (columns == null || columns.size() == 0)
			{
				return;
			}
			object[] nextNotEmptyNode = ColumnlistCommaProcessor.getNextNotEmptyNode(columns, 0);
			TParseTreeNode tparseTreeNode = (TParseTreeNode)nextNotEmptyNode[0];
			int num = (int)nextNotEmptyNode[1];
			TSourceToken token = null;
			if (tparseTreeNode != null && tparseTreeNode.startToken != null)
			{
				token = tparseTreeNode.startToken;
			}
			int length = SourceTokenOperator.curColumnNumberVT(token);
			int num2 = num;
			TParseTreeNode tparseTreeNode2 = tparseTreeNode;
			for (;;)
			{
				TParseTreeNode tparseTreeNode3 = tparseTreeNode2;
				object[] nextNotEmptyNode2 = ColumnlistCommaProcessor.getNextNotEmptyNode(columns, num2 + 1);
				tparseTreeNode2 = (TParseTreeNode)nextNotEmptyNode2[0];
				num2 = (int)nextNotEmptyNode2[1];
				if (tparseTreeNode2 == null)
				{
					break;
				}
				if (tparseTreeNode3.startToken != null && tparseTreeNode3.endToken != null && tparseTreeNode2.startToken != null && tparseTreeNode2.endToken != null)
				{
					TSourceTokenList container = tparseTreeNode3.startToken.container;
					if (alignStype == TAlignStyle.AsWrapped)
					{
						if (p == TLinefeedsCommaOption.LfbeforeCommaWithSpace)
						{
							SourceTokenOperator.addBefore(opt, tparseTreeNode2.startToken, SourceTokenConstant.WHITESPACE);
						}
					}
					else if (p == TLinefeedsCommaOption.LfAfterComma)
					{
						TSourceToken tsourceToken = SourceTokenSearcher.lastNotWhitespaceAndReturnToken(container, tparseTreeNode3.endToken.posinlist, tparseTreeNode2.startToken.posinlist);
						if (tsourceToken == null)
						{
							break;
						}
						if (tsourceToken.tokentype == ETokenType.ttcomma)
						{
							SourceTokenOperator.removeWhitespaceAndReturnFromEnd(opt, container, tsourceToken.posinlist);
							SourceTokenOperator.removeWhitespaceAndReturnFromStart(opt, container, tsourceToken.posinlist + 1);
						}
						SourceTokenOperator.addBefore(opt, tparseTreeNode2.startToken, SourceTokenOperator.createReturnSourceToken());
						SourceTokenOperator.addBefore(opt, tparseTreeNode2.startToken, SourceTokenOperator.createWhitespaceSourceToken(length));
					}
					else if (p == TLinefeedsCommaOption.LfbeforeCommaWithSpace || p == TLinefeedsCommaOption.LfBeforeComma)
					{
						TSourceToken tsourceToken2 = SourceTokenSearcher.lastNotWhitespaceAndReturnToken(container, tparseTreeNode3.endToken.posinlist, tparseTreeNode2.startToken.posinlist);
						if (tsourceToken2 == null)
						{
							break;
						}
						if (tsourceToken2.tokentype == ETokenType.ttcomma)
						{
							SourceTokenOperator.removeWhitespaceAndReturnFromEnd(opt, container, tsourceToken2.posinlist);
							SourceTokenOperator.removeWhitespaceAndReturnFromStart(opt, container, tsourceToken2.posinlist + 1);
							SourceTokenOperator.addBefore(opt, tsourceToken2, SourceTokenOperator.createReturnSourceToken());
							SourceTokenOperator.addBefore(opt, tsourceToken2, SourceTokenOperator.createWhitespaceSourceToken(length));
							if (p == TLinefeedsCommaOption.LfbeforeCommaWithSpace)
							{
								SourceTokenOperator.addAfter(opt, tsourceToken2, SourceTokenConstant.WHITESPACE);
							}
						}
						else if (tsourceToken2.astext != null && tsourceToken2.astext.Equals("distinct", StringComparison.OrdinalIgnoreCase))
						{
							SourceTokenOperator.removeWhitespaceAndReturnFromStart(opt, container, tsourceToken2.posinlist + 1);
							SourceTokenOperator.addAfter(opt, tsourceToken2, SourceTokenOperator.createReturnSourceToken());
						}
					}
				}
			}
		}

		// Token: 0x0600132C RID: 4908 RVA: 0x0031D40C File Offset: 0x0031B60C
		public override void process(TParseTreeNode node)
		{
			TParseTreeNodeList columns = node as TParseTreeNodeList;
			TLinefeedsCommaOption parameter = this.getParameter<TLinefeedsCommaOption>(typeof(TLinefeedsCommaOption), 0);
			TAlignStyle parameter2 = this.getParameter<TAlignStyle>(typeof(TAlignStyle), 1);
			ColumnlistCommaProcessor.processColumns(this.Option, parameter, parameter2, columns);
		}
	}
}
